home *** CD-ROM | disk | FTP | other *** search
/ 17 Bit Software 6: Level 6 / 17 Bit - Level 6 (1998)(Epic Marketing)[!].iso / quartz / q0430.dms / q0430.adf / Patch / inp.c < prev    next >
C/C++ Source or Header  |  1992-09-02  |  10KB  |  328 lines

  1. /* $Header: inp.c,v 2.0.1.1 88/06/03 15:06:13 lwall Locked $
  2.  *
  3.  * $Log:        inp.c,v $
  4.  * Revision 2.0.1.1  88/06/03  15:06:13  lwall
  5.  * patch10: made a little smarter about sccs files
  6.  *
  7.  * Revision 2.0  86/09/17  15:37:02  lwall
  8.  * Baseline for netwide release.
  9.  *
  10.  */
  11.  
  12. #include "EXTERN.h"
  13. #include "common.h"
  14. #include "util.h"
  15. #include "pch.h"
  16. #include "INTERN.h"
  17. #include "inp.h"
  18.  
  19. /* Input-file-with-indexable-lines abstract type */
  20.  
  21. static long i_size;                     /* size of the input file */
  22. static char *i_womp;                    /* plan a buffer for entire file */
  23. static char **i_ptr;                    /* pointers to lines in i_womp */
  24.  
  25. static int tifd = -1;                   /* plan b virtual string array */
  26. static char *tibuf[2];                  /* plan b buffers */
  27. static LINENUM tiline[2] = {-1, -1};    /* 1st line in each buffer */
  28. static LINENUM lines_per_buf;           /* how many lines per buffer */
  29. static int tireclen;                    /* length of records in tmp file */
  30.  
  31. /* New patch--prepare to edit another file. */
  32.  
  33. void
  34. re_input()
  35. {
  36.     if (using_plan_a) {
  37.         i_size = 0;
  38. #ifndef lint
  39.         if (i_ptr != Null(char**))
  40.             free((char *)i_ptr);
  41. #endif
  42.         if (i_womp != Nullch)
  43.             free(i_womp);
  44.         i_womp = Nullch;
  45.         i_ptr = Null(char **);
  46.     }
  47.     else {
  48.         using_plan_a = TRUE;            /* maybe the next one is smaller */
  49.         Close(tifd);
  50.         tifd = -1;
  51.         free(tibuf[0]);
  52.         free(tibuf[1]);
  53.         tibuf[0] = tibuf[1] = Nullch;
  54.         tiline[0] = tiline[1] = -1;
  55.         tireclen = 0;
  56.     }
  57. }
  58.  
  59. /* Constuct the line index, somehow or other. */
  60.  
  61. void
  62. scan_input(filename)
  63. char *filename;
  64. {
  65.     if (!plan_a(filename))
  66.         plan_b(filename);
  67.     if (verbose) {
  68.         say3("Patching file %s using Plan %s...\n", filename,
  69.           (using_plan_a ? "A" : "B") );
  70.     }
  71. }
  72.  
  73. /* Try keeping everything in memory. */
  74.  
  75. bool
  76. plan_a(filename)
  77. char *filename;
  78. {
  79.     int ifd;
  80.     Reg1 char *s;
  81.     Reg2 LINENUM iline;
  82.  
  83.     if (ok_to_create_file && stat(filename, &filestat) < 0) {
  84.         if (verbose)
  85.             say2("(Creating file %s...)\n",filename);
  86.         makedirs(filename, TRUE);
  87. #ifdef AMIGA
  88.         close(open(filename,O_WRONLY+O_CREAT)); /* creat don't work? */
  89. #else
  90.         close(creat(filename, 0666));
  91. #endif
  92.     }
  93.     if (stat(filename, &filestat) < 0) {
  94. #ifndef AMIGA   /* Amiga doesn't have RCS, so don't bother: */
  95.         Sprintf(buf, "RCS/%s%s", filename, RCSSUFFIX);
  96.         if (stat(buf, &filestat) >= 0 || stat(buf+4, &filestat) >= 0) {
  97.             Sprintf(buf, CHECKOUT, filename);
  98.             if (verbose)
  99.                 say2("Can't find %s--attempting to check it out from RCS.\n",
  100.                     filename);
  101.             if (system(buf) || stat(filename, &filestat))
  102.                 fatal2("Can't check out %s.\n", filename);
  103.         }
  104.         else {
  105.             Sprintf(buf+20, "SCCS/%s%s", SCCSPREFIX, filename);
  106.             if (stat(s=buf+20, &filestat) >= 0 ||
  107.               stat(s=buf+25, &filestat) >= 0) {
  108.                 Sprintf(buf, GET, s);
  109.                 if (verbose)
  110.                     say2("Can't find %s--attempting to get it from SCCS.\n",
  111.                         filename);
  112.                 if (system(buf) || stat(filename, &filestat))
  113.                     fatal2("Can't get %s.\n", filename);
  114.             }
  115.             else
  116. #endif
  117.                 fatal2("Can't find %s.\n", filename);
  118.         }
  119. #ifndef AMIGA
  120.     }
  121.     filemode = filestat.st_mode;
  122.     if ((filemode & S_IFMT) & ~S_IFREG)
  123.         fatal2("%s is not a normal file--can't patch.\n", filename);
  124. #endif
  125.     i_size = filestat.st_size;
  126.     if (out_of_mem) {
  127.         set_hunkmax();          /* make sure dynamic arrays are allocated */
  128.         out_of_mem = FALSE;
  129.         return FALSE;                   /* force plan b because plan a bombed */
  130.     }
  131. #ifdef lint
  132.     i_womp = Nullch;
  133. #else
  134.     i_womp = malloc((MEM)(i_size+2));   /* lint says this may alloc less than */
  135.                                         /* i_size, but that's okay, I think. */
  136. #endif
  137.     if (i_womp == Nullch)
  138.         return FALSE;
  139.     if ((ifd = open(filename, 0)) < 0)
  140.         fatal2("Can't open file %s\n", filename);
  141. #ifndef lint
  142.     if (read(ifd, i_womp, (int)i_size) != i_size) {
  143.         Close(ifd);     /* probably means i_size > 15 or 16 bits worth */
  144.         free(i_womp);   /* at this point it doesn't matter if i_womp was */
  145.         return FALSE;   /*   undersized. */
  146.     }
  147. #endif
  148.     Close(ifd);
  149.     if (i_size && i_womp[i_size-1] != '\n')
  150.         i_womp[i_size++] = '\n';
  151.     i_womp[i_size] = '\0';
  152.  
  153.     /* count the lines in the buffer so we know how many pointers we need */
  154.  
  155.     iline = 0;
  156.     for (s=i_womp; *s; s++) {
  157.         if (*s == '\n')
  158.             iline++;
  159.     }
  160. #ifdef lint
  161.     i_ptr = Null(char**);
  162. #else
  163.     i_ptr = (char **)malloc((MEM)((iline + 2) * sizeof(char *)));
  164. #endif
  165.     if (i_ptr == Null(char **)) {       /* shucks, it was a near thing */
  166.         free((char *)i_womp);
  167.         return FALSE;
  168.     }
  169.  
  170.     /* now scan the buffer and build pointer array */
  171.  
  172.     iline = 1;
  173.     i_ptr[iline] = i_womp;
  174.     for (s=i_womp; *s; s++) {
  175.         if (*s == '\n')
  176.             i_ptr[++iline] = s+1;       /* these are NOT null terminated */
  177.     }
  178.     input_lines = iline - 1;
  179.  
  180.     /* now check for revision, if any */
  181.  
  182.     if (revision != Nullch) {
  183.         if (!rev_in_string(i_womp)) {
  184.             if (force) {
  185.                 if (verbose)
  186.                     say2(
  187. "Warning: this file doesn't appear to be the %s version--patching anyway.\n",
  188.                         revision);
  189.             }
  190.             else {
  191.                 ask2(
  192. "This file doesn't appear to be the %s version--patch anyway? [n] ",
  193.                     revision);
  194.             if (*buf != 'y')
  195.                 fatal1("Aborted.\n");
  196.             }
  197.         }
  198.         else if (verbose)
  199.             say2("Good.  This file appears to be the %s version.\n",
  200.                 revision);
  201.     }
  202.     return TRUE;                        /* plan a will work */
  203. }
  204.  
  205. /* Keep (virtually) nothing in memory. */
  206.  
  207. void
  208. plan_b(filename)
  209. char *filename;
  210. {
  211.     Reg3 FILE *ifp;
  212.     Reg1 int i = 0;
  213.     Reg2 int maxlen = 1;
  214.     Reg4 bool found_revision = (revision == Nullch);
  215.  
  216.     using_plan_a = FALSE;
  217.     if ((ifp = fopen(filename, "r")) == Nullfp)
  218.         fatal2("Can't open file %s\n", filename);
  219.     if ((tifd = creat(TMPINNAME, 0666)) < 0)
  220.         fatal2("Can't open file %s\n", TMPINNAME);
  221.     while (fgets(buf, sizeof buf, ifp) != Nullch) {
  222.         if (revision != Nullch && !found_revision && rev_in_string(buf))
  223.             found_revision = TRUE;
  224.         if ((i = strlen(buf)) > maxlen)
  225.             maxlen = i;                 /* find longest line */
  226.     }
  227.     if (revision != Nullch) {
  228.         if (!found_revision) {
  229.             if (force) {
  230.                 if (verbose)
  231.                     say2(
  232. "Warning: this file doesn't appear to be the %s version--patching anyway.\n",
  233.                         revision);
  234.             }
  235.             else {
  236.                 ask2(
  237. "This file doesn't appear to be the %s version--patch anyway? [n] ",
  238.                     revision);
  239.                 if (*buf != 'y')
  240.                     fatal1("Aborted.\n");
  241.             }
  242.         }
  243.         else if (verbose)
  244.             say2("Good.  This file appears to be the %s version.\n",
  245.                 revision);
  246.     }
  247.     Fseek(ifp, 0L, 0);          /* rewind file */
  248.     lines_per_buf = BUFFERSIZE / maxlen;
  249.     tireclen = maxlen;
  250.     tibuf[0] = malloc((MEM)(BUFFERSIZE + 1));
  251.     tibuf[1] = malloc((MEM)(BUFFERSIZE + 1));
  252.     if (tibuf[1] == Nullch)
  253.         fatal1("Can't seem to get enough memory.\n");
  254.     for (i=1; ; i++) {
  255.         if (! (i % lines_per_buf))      /* new block */
  256.             if (write(tifd, tibuf[0], BUFFERSIZE) < BUFFERSIZE)
  257.                 fatal1("patch: can't write temp file.\n");
  258.         if (fgets(tibuf[0] + maxlen * (i%lines_per_buf), maxlen + 1, ifp)
  259.           == Nullch) {
  260.             input_lines = i - 1;
  261.             if (i % lines_per_buf)
  262.                 if (write(tifd, tibuf[0], BUFFERSIZE) < BUFFERSIZE)
  263.                     fatal1("patch: can't write temp file.\n");
  264.             break;
  265.         }
  266.     }
  267.     Fclose(ifp);
  268.     Close(tifd);
  269.     if ((tifd = open(TMPINNAME, 0)) < 0) {
  270.         fatal2("Can't reopen file %s\n", TMPINNAME);
  271.     }
  272. }
  273.  
  274. /* Fetch a line from the input file, \n terminated, not necessarily \0. */
  275.  
  276. char *
  277. ifetch(line,whichbuf)
  278. Reg1 LINENUM line;
  279. int whichbuf;                           /* ignored when file in memory */
  280. {
  281.     if (line < 1 || line > input_lines)
  282.         return "";
  283.     if (using_plan_a)
  284.         return i_ptr[line];
  285.     else {
  286.         LINENUM offline = line % lines_per_buf;
  287.         LINENUM baseline = line - offline;
  288.  
  289.         if (tiline[0] == baseline)
  290.             whichbuf = 0;
  291.         else if (tiline[1] == baseline)
  292.             whichbuf = 1;
  293.         else {
  294.             tiline[whichbuf] = baseline;
  295. #ifndef lint            /* complains of long accuracy */
  296.             Lseek(tifd, (long)baseline / lines_per_buf * BUFFERSIZE, 0);
  297. #endif
  298.             if (read(tifd, tibuf[whichbuf], BUFFERSIZE) < 0)
  299.                 fatal2("Error reading tmp file %s.\n", TMPINNAME);
  300.         }
  301.         return tibuf[whichbuf] + (tireclen*offline);
  302.     }
  303. }
  304.  
  305. /* True if the string argument contains the revision number we want. */
  306.  
  307. bool
  308. rev_in_string(string)
  309. char *string;
  310. {
  311.     Reg1 char *s;
  312.     Reg2 int patlen;
  313.  
  314.     if (revision == Nullch)
  315.         return TRUE;
  316.     patlen = strlen(revision);
  317.     if (strnEQ(string,revision,patlen) && isspace(s[patlen]))
  318.         return TRUE;
  319.     for (s = string; *s; s++) {
  320.         if (isspace(*s) && strnEQ(s+1, revision, patlen) &&
  321.                 isspace(s[patlen+1] )) {
  322.             return TRUE;
  323.         }
  324.     }
  325.     return FALSE;
  326. }
  327.  
  328.